package com.epwk.action;


import com.epwk.action.AlertAction;
import com.epwk.driver.SeleniumDriver;
import com.epwk.element.WebEle;
import com.epwk.logger.LoggerControler;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

import static com.epwk.driver.SeleniumDriver.driver;

/**
 * Created by Administrator on 2016/11/13.
 */
public class MouseAction extends SeleniumDriver {
    final static LoggerControler log= LoggerControler.getLogger(AlertAction.class);

    public static void rightClick(By by){
        WebElement webElement = WebEle.findElement(by);
        Actions action = new Actions(driver);
        action.contextClick(webElement).perform();
        log.info("在"+by+"处右击");
    }

    public static void doubleClick(By by){
        WebElement webElement = WebEle.findElement(by);
        Actions action = new Actions(driver);
        action.doubleClick(webElement).perform();
        log.info("在"+by+"处双击");
    }

    public static void moveToElement(By by){
        WebElement webElement = WebEle.findElement(by);
        Actions action = new Actions(driver);
        action.moveToElement(webElement).perform();
        log.info("将鼠标移至"+by+"处");
    }

    public static void dragAndDrop(By by,int x,int y){
        WebElement webElement = WebEle.findElement(by);
        Actions action = new Actions(driver);
        action.dragAndDropBy(webElement,x,y).perform();
        log.info("将元素"+by+"拖拽到("+x+","+y+")处");
    }

    public static void clickAndHold(By oneby,By twoby){
        WebElement oneElement = WebEle.findElement(oneby);
        WebElement twoElement = WebEle.findElement(twoby);
        Actions action = new Actions(driver);
        action.clickAndHold(oneElement).moveToElement(twoElement).release(oneElement).perform();
        log.info("将元素"+oneby+"拖到"+twoby+"处");
    }

    public static void multiSelect(By select,By option,int oneid,int twoid){
        WebElement selection = WebEle.findElement(select);
        List<WebElement> options = selection.findElements(option);
        Actions builder = new Actions(driver);
        builder.keyDown(Keys.SHIFT).click(options.get(oneid)).click(options.get(twoid)).perform();
        log.info("选择多选框中的"+(oneid+1)+"和"+(twoid+1)+"选项");

    }

    public static void selectValue(By by,int i){
        WebElement cashSelect = driver.findElement(by);
        Select select = new Select(cashSelect);
        select.selectByIndex(i);
        log.info("用户选择了第"+i+"个选项");
    }


}
